package si.cir.util;

import java.util.TreeMap;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class PredmetiHandler extends DefaultHandler{
	
	private TreeMap<String, String> predmeti = new TreeMap<String, String>();
	private String currentElement = "";
	private String ime;
	private String id;
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if(qName.equals("predmet")){
			ime = null;
			id = null;
		}else if(qName.equals("ime"))
			currentElement = qName;
		else if(qName.equals("id"))
			currentElement = qName;
	}
	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if(qName.equals("predmet"))
			predmeti.put(id, ime);
		else if(qName.equals("ime"))
			currentElement = "";
		else if(qName.equals("id"))
			currentElement = "";
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if(currentElement.equals("ime"))
			ime = new String(ch, start, length);
		else if(currentElement.equals("id"))
			id = new String(ch, start, length);
	}
	
	public TreeMap<String, String> getPredmeti(){
		return predmeti;
	}
}

